package br.ucb.tcc.validation;

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.border.Border;

/**
 * @author italoricci
 * 
 */
public abstract class Validator extends InputVerifier {

	private Border originalBorder;
	private Color originalBackgroundColor;
	private String originalTooltipText;

	public Validator(JComponent c) {
		originalBorder = c.getBorder();
		originalBackgroundColor = c.getBackground();
		originalTooltipText = c.getToolTipText();

	}

	public Validator(JFrame parent, JComponent c) {
		this(c);
	}

	public Validator(JDialog parent, JComponent c) {
		this(c);
	}

	protected abstract Erro definirErro(JComponent c);

	@Override
	public boolean verify(JComponent c) {
		Erro error = definirErro(c);
		if (error.getErrorType() == Erro.NO_ERROR) {
			c.setBackground(originalBackgroundColor);
			c.setBorder(originalBorder);
			c.setToolTipText(originalTooltipText);
			return true;
		} else {
			c.setBorder(new IconBorder(error.getImage(), BorderFactory.createLineBorder(originalBackgroundColor)));
			c.setBackground(error.getColor());
			c.setToolTipText(error.getMessage());
			return false;
		}
	}
}
